package dao;

import entity.User;
import util.UserUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDao extends BaseDao {
    public UserDao() {
        super();
    }

    /**
     * 储存用户信息，可用于实现注册功能
     *
     * @param user
     */
    public void save(User user) {
        String sql = "INSERT INTO t_user(username, password, email, phone, created, updated) VALUES(?, ?, ?, ?, NOW(), NOW())";
        // 拿到刚刚插入的数据的id
        int id = this.updateData(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getPhone());
        user.setId(id);
    }

    /**
     * 根据id修改密码
     *
     * @param user
     */
    public void updatePassword(User user) {
        String sql = "UPDATE t_user SET password = ?, updated = NOW() WHERE id = ?";
        this.updateData(sql, user.getPassword(), user.getId());
    }

    /**
     * 根据id修改用户邮箱和手机号
     *
     * @param user
     */
    public void updateEmailAndPhone(User user) {
        String sql = "UPDATE t_user SET email = ?, phone = ?, updated = NOW() WHERE id = ?";
        this.updateData(sql, user.getEmail(), user.getPhone(), user.getId());
    }

    /**
     * 根据用户名和密码查询用户
     */
    public User findByUsernameAndPassword(String username, String password) {
        String sql = "SELECT * FROM t_user WHERE username = ? AND password = ?";
        User user = null;
        try (ResultSet rs = this.queryData(sql, username, password)) {
            if (rs != null && rs.next()) {
                user = UserUtil.rsToUser(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * 查询用户名是否存在
     */
    public boolean hasUsername(String username) {
        String sql = "SELECT username FROM t_user WHERE username = ?";
        boolean flag = false;
        try (ResultSet rs = this.queryData(sql, username)) {
            if (rs != null && rs.next()) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 查找邮箱是否存在
     */
    public boolean hasEmail(String email) {
        String sql = "SELECT email FROM t_user WHERE email = ?";
        try (ResultSet rs = this.queryData(sql, email)) {
            if (rs != null && rs.next()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查找手机号是否存在
     */
    public boolean hasPhone(String phone) {
        String sql = "SELECT phone FROM t_user WHERE phone = ?";
        try (ResultSet rs = this.queryData(sql, phone)) {
            if (rs != null && rs.next()) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 查询所有用户
     */
    public List<User> findAll() {
        String sql = "SELECT * FROM t_user";
        List<User> list;
        try (ResultSet rs = this.queryData(sql)) {
            if (rs == null) {
                return null;
            }
            list = new ArrayList<>();
            while (rs.next()) {
                User user = UserUtil.rsToUser(rs);
                list.add(user);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
